package Fisica;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Fisica.Conexion;
import Logica.Util;
import Modelo.TarjetaCompra;
//import Modelo.Articulo;
import Modelo.TarjetaExtencion;

public class DAOExtencion {
	
	public static String SELECT = "select * from tarjeta_extencion";	
	public static String FIRST = "SELECT * FROM tarjeta_extencion order by id asc;";
	public static String LAST = "SELECT * FROM tarjeta_extencion order by id desc;";
	public static String PRE = "SELECT * FROM tarjeta_extencion WHERE id < ? order by id desc";
	public static String POST = "SELECT * FROM tarjeta_extencion WHERE id > ? order by id asc";
	public static String INSERT = "INSERT INTO tarjeta_extencion (id_tarjeta, nombre, apellido, domicilio, dni) VALUES (?,?,?,?,?);";
	public static String UPDATE = "UPDATE tarjeta_extencion SET nombre = ?, apellido = ?, domicilio = ?, dni = ? where id_tarjeta = ?;";
	
	
	public static Modelo.TarjetaExtencion get (int id_tarjeta){
		
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(SELECT+" where id_tarjeta = ?;");
			st.setInt(1, id_tarjeta);
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Modelo.TarjetaExtencion a = new Modelo.TarjetaExtencion(); 
				a.setId_tarjeta(rs.getInt("id_tarjeta")); 	
				a.setNombre(rs.getString("nombre"));
				a.setApellido(rs.getString("apellido"));
				a.setDomicilio(rs.getString("domicilio"));
				a.setDni(rs.getString("dni"));
								
				return a;
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return new TarjetaExtencion();
	}
	

	public static List<Object> getList (){
		List<Object> list = new ArrayList();
		String sql = SELECT +";";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			TarjetaExtencion a;
			while (rs.next()){				
				a = new Modelo.TarjetaExtencion(); 
				a.setId_tarjeta(rs.getInt("id_tarjeta")); 	
				a.setNombre(rs.getString("nombre"));
				a.setApellido(rs.getString("apellido"));
				a.setDomicilio(rs.getString("domicilio"));
				a.setDni(rs.getString("dni"));
				
				list.add(a);
			}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 				
		return list;
	}
	
	 public static boolean insert(Object obj) { //ok
		 TarjetaExtencion v = (TarjetaExtencion)obj;
//INSERT INTO tarjeta_extencion (id_tarjeta, nombre, apellido, domicilio, dni) 
//VALUES (1234, 'luciano','centis','san martin 11','22333444');
			try {			
				PreparedStatement st;
				ResultSet rs;
				//INSERTO
				st = Conexion.getConexion().prepareStatement(INSERT);	
				st.setInt(1, v.getId_tarjeta());
				st.setString(2, v.getNombre());
				st.setString(3, v.getApellido());
				st.setString(4, v.getDomicilio());
				st.setString(5, v.getDni());
				
				st.executeUpdate();
				//RECUPERO
				st = Conexion.getConexion().prepareStatement(SELECT + " where id_tarjeta = ? and dni = ?;");
				st.setInt(1, v.getId_tarjeta());
				st.setString(2, v.getDni());
								
				rs = st.executeQuery();
				if(rs.next())
					v.setId(rs.getInt("id"));
				System.out.println("inserto: "+v.getId());

				return true;
				
			}catch(Exception e){
				System.out.println("exception al intentar insertar: "+v.getId());
				e.printStackTrace();
			}
			return false;				
		}
	

	 
	 public static boolean update(Object obj) { //ok
		 TarjetaExtencion v = (TarjetaExtencion)obj;
//		 UPDATE tarjeta_extencion 
//		 SET nombre = 'hector', apellido = 'victor', domicilio = 'pbro sponton 999', dni = '99888777' 
//		 where id_tarjeta = 1234;
		 try {			
				PreparedStatement st;
				ResultSet rs;
				//INSERTO
				st = Conexion.getConexion().prepareStatement(UPDATE);	
				st.setString(1, v.getNombre());
				st.setString(2, v.getApellido());
				st.setString(3, v.getDomicilio());
				st.setString(4, v.getDni());
				st.setInt(5, v.getId_tarjeta());
				
				st.executeUpdate();
				//RECUPERO
				st = Conexion.getConexion().prepareStatement(SELECT + " where id_tarjeta = ? and dni = ?;");
				st.setInt(1, v.getId_tarjeta());
				st.setString(2, v.getDni());
								
				rs = st.executeQuery();
				if(rs.next())
					v.setId(rs.getInt("id"));
				System.out.println("actualizo: "+v.getId());

				return true;
				
			}catch(Exception e){
				System.out.println("exception al actualizar insertar: "+v.getId());
				e.printStackTrace();
			}
			return false;				
		} 
	 
	public static void main(String[] args) {
		DAOExtencion p = new DAOExtencion();
//		p.getArticulo();


	}

}
